package cn.kjcoder.redis.controller;

import cn.kjcoder.redis.domain.WeiboList;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@RestController
@Slf4j
@RequestMapping("/recommended")
public class WeiboListController {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String RECOMMENDED_KEY = "list:recommended";

    @GetMapping("/list")
    public WeiboList list(){
        WeiboList weiboList = null;
        try {
            weiboList = (WeiboList) this.redisTemplate.opsForSet().randomMember(RECOMMENDED_KEY);
            log.info("查询结果：{}", weiboList);
        } catch (Exception e) {
            //这里的异常，一般是redis瘫痪 ，或 redis网络timeout
            log.error("exception:", e);
            //TODO 去数据库中查
        }
        return weiboList;
    }
}
